var zAsyncArr = [];//设置ztree缓存机制，已加载过就不需要重复加载
(function(){
	var _treeObj=null;
	document.ztree = function(treeObj,url,configure,menu){
		_treeObj = treeObj;
		var menuId = window.localStorage? localStorage.getItem("nid"): Cookie.read("nid");
		var setting = {};
		
		if(typeof (configure) == "object" && Object.prototype.toString.call(configure).toLowerCase() == "[object object]" && !configure.length){
			setting = configure;
			defaultAsync.url = url;
			if(menu)menuId = menu;
			defaultAsync.otherParam = {"menuId":menuId};
			if(!setting.async)setting.async = defaultAsync;
			//if(!setting.callback)
				$.extend(defaultCallback,configure.callback);
				setting.callback = defaultCallback;
			if(!setting.check)setting.check = defaultCheck;
			if(!setting.data)setting.data = defaultData;
			if(!setting.edit)setting.edit = defaultEdit;
			if(!setting.view)setting.view = defaultView;
		}else{
			defaultAsync.url = url;
			defaultAsync.otherParam = {"menuId":menuId};
			setting = {
				async: defaultAsync
			}
		}
		return initTree(treeObj,setting);
	}
	
	/**
	 * @descrption 默认的ztree的请求配置方式.<br>
	 */
	var defaultAsync = {
		enable: true,
		type: "post",
		dataFilter: filter,
		autoParam:["id","name","pid"],
		otherParam:[]
	}, 
	
	/**
	 * @descrption 默认的ztree的回调.<br>
	 */
	defaultCallback = {
			//展开节点
			onExpand:function(event, treeId, treeNode){
				/**if($.inArray(treeNode.id,zAsyncArr) == -1) 贝国刚，为何加上这句话*/
				//if($.inArray(treeNode.id,zAsyncArr) == -1){
					/*根据 treeId 获取 zTree 对象*/
					var zTree = $.fn.zTree.getZTreeObj(_treeObj),
						type = "refresh",
						silent = false;
					/*强行异步加载父节点的子节点。[setting.async.enable = true 时有效]*/
					zTree.reAsyncChildNodes(treeNode, type, silent);
				//}
			},
			//异步加载完成
			onAsyncSuccess:function(event, treeId, treeNode, msg){
				if(treeNode && $.inArray(treeNode.id,zAsyncArr) == -1 ) zAsyncArr.push(treeNode.id);
			} 
	},
	
	/**
	 * @descrption 默认的ztree的勾选事件.<br>
	 */
	defaultCheck = {},
	
	/**
	 * @descrption 默认的ztree的勾选事件.<br>
	 */
	defaultData = {},
	
	/**
	 * @descrption 默认的ztree的勾选事件.<br>
	 */
	defaultEdit = {},
	
	/**
	 * @descrption 默认的ztree的勾选事件.<br>
	 */
	defaultView = {};
	
	function filter(treeId, parentNode, childNodes) {
		if (!childNodes) return null;
		for (var i=0, l=childNodes.length; i<l; i++) {
			childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
		}
		return childNodes;
	}
	
	/**
	 * @param treeObj 绑定的节点Id.<br>
	 * @param setting 参数配置.<br>
	 */
	function initTree(treeObj,setting){
		return $.fn.zTree.init($("#"+treeObj), setting);
	}
	
	if(!$.ztree){
		$.ztree = document.ztree;
	}
	if(!document.ztree){
		document.ztree = $.ztree;
	}
})();

/**
 * @description 刷新树的父节点.<br>
 * @param treeObj 树信息.<br>
 */
function refreshParentNode(treeObj,location) {
	if(!location){
		location = false;
	}
    var type = "refresh";  
    var silent = false;  
    var nodes = treeObj.getSelectedNodes();
    var node = nodes[0];
    if(!nodes.length){
    	node = null;
    	treeObj.reAsyncChildNodes(null, type, true);
    }else{
    	var	nodeId = node.id;
        var nodeArrIndex = zAsyncArr.indexOf(nodeId);
        var zAsyncArrLen = zAsyncArr.length - nodeArrIndex;
        //修复刷新节点时自节点无法加载的问题
        zAsyncArr.splice(nodeArrIndex,zAsyncArrLen);
        if(!node.isParent||location==false){
        	 /*根据 zTree 的唯一标识 tId 快速获取节点 JSON 数据对象*/
        	var parentNode = treeObj.getNodeByTId(nodes[0].parentTId);
        	/*选中指定节点*/
        	treeObj.selectNode(parentNode);
        	treeObj.reAsyncChildNodes(parentNode, type, silent);
        }else{
        	/*强行异步加载父节点的子节点。[setting.async.enable = true 时有效]*/
            treeObj.reAsyncChildNodes(nodes[0], type, silent);
        }
        
        var nodeId = nodes[0].parentTId;
        var parentNode = treeObj.getNodeByTId(nodeId);
    }
    if(location&&nodes.length>0){
    	nodeId = nodes[0].id;
    	parentNode = nodes[0];
    }

    /*
    var nodes = treeObj.getSelectedNodes();  
    根据 zTree 的唯一标识 tId 快速获取节点 JSON 数据对象
     
    treeObj.reAsyncChildNodes(parentNode, type, silent);  
   
    选中指定节点  
    treeObj.selectNode(parentNode); */
}
